package cn.jiyun.mongo.demo;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class MongoDemo {
    public static void main(String[] args) {
        //1、建立连接
        MongoClient client = new MongoClient("127.0.0.1");
        //2、打开数据库
        MongoDatabase spitdb = client.getDatabase("spitdb");
        //3、获取集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");
        //4、获取文档数据
        FindIterable<Document> documents = spit.find();
        for (Document document : documents) {
            System.out.println("内容："+	document.getString("content"));
            System.out.println("用户ID:"+document.getString("userid"));
            System.out.println("浏览量："+document.getInteger("visits"));
            System.out.println("=========================");
        }
        //5、关闭资源
        client.close();
    }
}
